package com.lqq;

import com.lqq.service.MyService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.sql.DataSource;

/**
 * @author lqq
 * @date 2018/12/6
 */
@SpringBootApplication
@EnableSwagger2
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class);
    }


    /**
     * spring启动之后执行的bean
     * @param repositoryService
     * @param runtimeService
     * @param taskService
     * @return
     */
    //@Bean
    //public CommandLineRunner init(final RepositoryService repositoryService,
    //                              final RuntimeService runtimeService,
    //                              final TaskService taskService) {
    //
    //    return new CommandLineRunner() {
    //        @Override
    //        public void run(String... strings) throws Exception {
    //            System.out.println("Number of process definitions : "
    //                    + repositoryService.createProcessDefinitionQuery().count());
    //            System.out.println("Number of tasks : " + taskService.createTaskQuery().count());
    //            runtimeService.startProcessInstanceByKey("oneTaskProcess");
    //            System.out.println("Number of tasks after process start: " + taskService.createTaskQuery().count());
    //        }
    //    };
    //}

    @Bean
    public CommandLineRunner init(final MyService myService) {

        return new CommandLineRunner() {
            @Override
            public void run(String... strings) throws Exception {
                myService.createDemoUsers();
            }
        };

    }

    @Bean
    public DataSource database() {
        return DataSourceBuilder.create()
                .url("jdbc:mysql://127.0.0.1:3306/activiti-spring-boot?characterEncoding=UTF-8")
                .username("root")
                .password("root")
                .driverClassName("com.mysql.jdbc.Driver")
                .build();
    }
}
